<template>  
  <div class="system-appVersionCtrl-add">
    <!-- 添加或修改app版本控制对话框 -->
    <el-dialog v-model="isShowDialog" width="769px" :close-on-click-modal="false" :destroy-on-close="true">
      <template #header>
        <div v-drag="['.system-appVersionCtrl-add .el-dialog', '.system-appVersionCtrl-add .el-dialog__header']">添加app版本控制</div>
      </template>
      <el-form ref="formRef" :model="formData" :rules="rules" label-width="90px">        
        <el-form-item label="版本号" prop="version">
          <el-input v-model="formData.version" placeholder="请输入版本号" />
        </el-form-item>        
        <el-form-item label="文件地址" prop="filepath" >
          <upload-file :action="baseURL+'api/v1/system/upload/singleFile'" v-model="formData.filepath" @upFileData="setUpFileListFilepath" :limit="1"></upload-file>
        </el-form-item>          
        <el-form-item label="是否启用" prop="status">
          <el-radio-group v-model="formData.status">
            <el-radio>请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>        
        <el-form-item label="更新内容" prop="description">
          <el-input v-model="formData.description" placeholder="请输入更新内容" />
        </el-form-item>       
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="onSubmit">确 定</el-button>
          <el-button @click="onCancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts">
import { reactive, toRefs, defineComponent,ref,unref,getCurrentInstance } from 'vue';
import {ElMessageBox, ElMessage, FormInstance,UploadProps} from 'element-plus';
import {
  listAppVersionCtrl,
  getAppVersionCtrl,
  delAppVersionCtrl,
  addAppVersionCtrl,
  updateAppVersionCtrl,  
} from "/@/api/system/appVersionCtrl";
import uploadFile from "/@/components/uploadFile/index.vue"
import {
  AppVersionCtrlTableColumns,
  AppVersionCtrlInfoData,
  AppVersionCtrlTableDataState,
  AppVersionCtrlEditState,  
} from "/@/views/system/appVersionCtrl/list/component/model"
export default defineComponent({
  name:"apiV1SystemAppVersionCtrlEdit",
  components:{    
    uploadFile,    
  },
  props:{    
  },
  setup(props,{emit}) {    
    const baseURL:string|undefined|boolean = import.meta.env.VITE_API_URL    
    const {proxy} = <any>getCurrentInstance()
    const formRef = ref<HTMLElement | null>(null);
    const menuRef = ref();    
    const state = reactive<AppVersionCtrlEditState>({
      loading:false,
      isShowDialog: false,
      formData: {        
        id: undefined,        
        version: undefined,        
        filepath: [] ,        
        status: false ,        
        description: undefined,        
        createdAt: undefined,        
      },
      // 表单校验
      rules: {        
        id : [
            { required: true, message: "不能为空", trigger: "blur" }
        ],        
        version : [
            { required: true, message: "版本号不能为空", trigger: "blur" }
        ],        
        filepath : [
            { required: true, message: "文件地址不能为空", trigger: "blur" }
        ],        
        status : [
            { required: true, message: "是否启用不能为空", trigger: "blur" }
        ],        
        description : [
            { required: true, message: "更新内容不能为空", trigger: "blur" }
        ],        
      }
    });
    // 打开弹窗
    const openDialog = () => {
      resetForm();
      state.isShowDialog = true;
    };
    // 关闭弹窗
    const closeDialog = () => {
      state.isShowDialog = false;
    };
    // 取消
    const onCancel = () => {
      closeDialog();
    };
    // 提交
    const onSubmit = () => {
      const formWrap = unref(formRef) as any;
      if (!formWrap) return;
      formWrap.validate((valid: boolean) => {
        if (valid) {
          state.loading = true;
          //添加
          addAppVersionCtrl(state.formData).then(()=>{
              ElMessage.success('添加成功');
              closeDialog(); // 关闭弹窗
              emit('appVersionCtrlList')
            }).finally(()=>{
              state.loading = false;
            })
        }
      });
    };
    const resetForm = ()=>{
      state.formData = {        
        id: undefined,        
        version: undefined,        
        filepath: [] ,        
        status: false ,        
        description: undefined,        
        createdAt: undefined,        
      }
    };    
    const setUpFileListFilepath = (data:any)=>{
      state.formData.filepath = data
    }    
    return {
      proxy,
      openDialog,
      closeDialog,
      onCancel,
      onSubmit,
      menuRef,
      formRef,      
      setUpFileListFilepath,      
      baseURL,      
      ...toRefs(state),
    };
  }
})
</script>
<style scoped>  
</style>